﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BookStore.Bussiness;
using BookStore.Data;
using System.Data.SqlClient;

namespace BookStore
{
    public partial class Checkcart : System.Web.UI.Page
    {
        productService proservic = new productService();
        Double Tongtien = 0;
        string fullname;
        string phone;
        string address;
        string email;
        string totalmoney;
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadDataToCart();
            //pnlcheck.Enabled = false;
            
  
        }
        public void LoadDataToCart()
        {
            Cart objCart = new Cart();
            if (Session["cart"] != null)
                objCart = (Cart)Session["cart"];
            DataList1.DataSource = objCart.arrCart;
            DataList1.DataBind();
            Double Tongtien = 0;

            //for (int i = 0; i < objCart.arrCart.Count; i++)
            //{
            //    lbltongtien.Text = i.ToString();
            //}
           
        }


        protected void DataList1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("Default.aspx");
        }

       
       

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            LinkButton lnb = (LinkButton)sender;
            int id = Convert.ToInt32(lnb.CommandArgument);
            productService biz = new productService();
            productInfo sp = biz.getById(id);
          
                Cart objCart = new Cart();
                if (Session["cart"] != null)
                    objCart = (Cart)Session["cart"];
                objCart.RemoveCart(sp);    
        }

        protected void LinkButton2_Click(object sender, EventArgs e)
        {
            LinkButton lnb = (LinkButton)sender;
            int id = Convert.ToInt32(lnb.CommandArgument);
            productService biz = new productService();
            productInfo sp = biz.getById(id);

            Cart objCart = new Cart();
            if (Session["cart"] != null)
                objCart = (Cart)Session["cart"];
            objCart.Update(sp);
            Session["cart"] = objCart;

        }

        protected void tmbcheck_Click(object sender, EventArgs e)
        {
            if (Session["Customer"] == null)
            {
                Response.Redirect("Login.aspx");
            }
            else
            {
                Panel1.Enabled = true;
            }
        }

        protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            TextBox txttong = (TextBox)e.Item.FindControl("txttong");
           
            Tongtien += double.Parse(txttong.Text);
            lbltongtien.Text =  Tongtien.ToString();
            //String.Format("{0:C}", 123.0); // “123.00″
            lbltongtien.Text = string.Format(System.Globalization.CultureInfo.CreateSpecificCulture("en-US"), "{0:$#,##.#0}", Tongtien.ToString());

            

        }
        string id;
        bool test;
        int orderid;
        DataTable dt;
        string proid;
        protected void Button1_Click(object sender, EventArgs e)
        {
            foreach (DataListItem item in this.DataList1.Items)
            {
                Label proid = (Label)item.FindControl("lblid");
                id = proid.Text;

            }

           // Response.Write(id);
            SqlConnection con = Data.sqlDataProvider.getConnect();
            string str = TextBox1.Text;
            SqlCommand cmd = new SqlCommand("SELECT customerID FROM tblCustomer  WHERE email = '" + Session["Customer"] + "'", con);
            //cmd.CommandType = CommandType.StoredProcedure;
            
            //cmd.Parameters.AddWithValue("@email", str);
           // SqlDataAdapter adapter = new SqlDataAdapter(cmd);

            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    lblID.Text = dr.GetInt32(0).ToString();
                }
            }
            dr.Close();
            //dt = new DataTable();

            int idcus = Convert.ToInt32(lblID.Text);
           // Response.Write(idcus);
            orderDetailInfo objOD = new orderDetailInfo();
            orderInfo objOrder = new orderInfo();
            // orderid = objOrder.orderID;

            objOrder.customerID = idcus;
            objOrder.dateorder = DateTime.Now.Date;
            objOrder.datereceipt = DateTime.Now.Date;
            objOrder.addressDelivery = "";
            objOrder.phone = "2312412";
            objOrder.paymentID = 1;
            objOrder.totalMoney = lbltongtien.Text;
            objOrder.status = true;
            objOrder.deliveryCharges = "21";
            // ket thuc

            SqlDataAdapter data = new SqlDataAdapter("selectTop_idorder", con);
            data.SelectCommand.CommandType = CommandType.StoredProcedure;
            //dt = new DataTable();
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);

            SqlDataReader dr1 = cmd.ExecuteReader();
            if (dr1.HasRows)
            {
                while (dr.Read())
                {
                    proid = dr1.GetInt32(0).ToString();
                }
            }
            dr1.Close();
            int xyz = Convert.ToInt32(proid);

            objOD.orderID = xyz;
            objOD.productID = Convert.ToInt32(id);
            //objOD.quantity = Convert.ToInt32(txtquantity.Text);
            objOD.price = "23434";
            //objOD.status = rdbStatus.Checked;
            test = orderService.insertOrder(objOrder);
            test = orderDetailService.insertOrderDetail(objOD);

            if (test == true)
            {
                Response.Write("<script language='javascript'>alert('insert success');</script>");
            }
            else
            {
                Response.Write("<script language='javascript'>alert('insert failed');</script>");
            }
        }
        
       

      
    }
}
